﻿using Wp7Tools.ViewServices;

namespace Wp7Tools.Services {
	public class BusyService {
		private static IBusyService _default;
		public static IBusyService Default {
			get { return _default; }
			set {
				_default = value;
				lock (_sync) {
					_default.IsBusy = (_counter > 0);
				}
			}
		}

		private static object _sync = new object();
		private static int _counter;

		public static void IncreaseBusyCounter(int times = 1) {
			lock (_sync) {
				_counter += times;
				if (_default == null) return;
				Default.IsBusy = true;
			}
		}

		public static void DecreaseBusyCounter() {
			lock (_sync) {
				_counter--;
				if (_counter <= 0 && _default != null) {
					Default.IsBusy = false;
				}
			}
		}
	}
}
